package br.com.toscano.gobra.persistence;

import java.util.List;

import javax.persistence.Query;

import br.com.toscano.gobra.domain.ObjetoAbstrato;
import br.gov.frameworkdemoiselle.stereotype.PersistenceController;
import br.gov.frameworkdemoiselle.template.JPACrud;

@PersistenceController
public abstract class AbstractDAO<OBJ extends ObjetoAbstrato, KEY extends Object> extends JPACrud<OBJ, KEY> {

	private static final long serialVersionUID = 1L;
	
	public OBJ consultarPorDescricao(String parametro, Object valor) {
		Query q = createQuery("from " + this.getBeanClass().getSimpleName() + " obj where obj." + parametro + " = :" + parametro + "" );
		q.setParameter(parametro, valor);
		List<OBJ> lista = q.getResultList();
		if (lista != null && lista.size()>0) 
			return lista.iterator().next();
		return null;
	}
	
}
